/**
 * 
 */
package com.testvideo.command;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.testvideo.util.LangUtil;


/**
 * @author Administrator
 * 当前播放视频的即时信息。
 */
public class VideoClipInfo {
	protected LineNumberReader sdi ;
	protected OutputStreamWriter sdo ;
	protected ReaderThread readerThread ;
	
	private List<ClipInfoChangeListener> listeners = new LinkedList<ClipInfoChangeListener>() ;
	
	private Map<String, String> props = new HashMap<String, String>() ;
	
	public VideoClipInfo() {
		
	}
	
	public void changeStream(InputStreamReader sdi, OutputStreamWriter sdo){
		if(readerThread != null){
			readerThread.quit() ;
		}
		
		if(sdi != null){
			this.sdi = new LineNumberReader(sdi) ;
			this.sdo = sdo ;
			
			readerThread = new ReaderThread() ;
			readerThread.setDaemon(true) ;
			readerThread.start() ;
		}
	}
	
	public void clearVideoProps(){
		//TODO: notify listeners to update this.
		props.clear() ;
	}
	
	public int getWidth(){
		return LangUtil.string2Int(props.get("ANS_width")) ;
	}
	
	public int getHeight(){
		return LangUtil.string2Int(props.get("ANS_height")) ;
	}
	
	public long getLengthInSeconds(){
		return LangUtil.string2Int(props.get("ANS_stream_length")) ;
	}
	
	public float getVolume(){
		return LangUtil.string2Float(props.get("ANS_volume")) ;
	}
	
	public float getAsFloat(String key){
		return LangUtil.string2Float(props.get(key)) ;
	}
	
	public long getAsLong(String key){
		return LangUtil.string2Long(props.get(key)) ;
	}
	
	public void notifyNewEvent(String key, String value){
		processNewLineOutput(key + "=" + value) ;
	}
	
	protected void processNewLineOutput(String line){
		System.out.println(line);
		//读取key=value类型的数据。
		int pos = line.indexOf('=') ;
		if(pos < 0) return ;
				
		String key = line.substring(0, pos).trim() ;
		if(key.length() == 0) return ;
		
		String value = line.substring(pos + 1).trim() ;
				
		String oldValue = props.get(key) ;
		
		//值没有变化。
		//new 无论有没有变化都需要传递下去。
//		if(oldValue != null && oldValue.equals(value)){
//			return ;
//		}			
				
		props.put(key, value) ;
		
		//notify listeners.
		ClipInfoChangeEvent evt = new ClipInfoChangeEvent(key, value) ;
		for(ClipInfoChangeListener l : listeners){
			l.updateClipInfo(evt) ;
		}
		
		System.out.println("updating " + key + "==>" + value) ;
	}
	
	public void addClipInfoChangeListener(ClipInfoChangeListener l){
		this.listeners.add(l) ;
	}
	
	private class ReaderThread extends Thread{
		private boolean keepRunning = true ;
		
		public void run(){
			while(keepRunning){
				try {
					String line = sdi.readLine() ;
					if(line != null){
						VideoClipInfo.this.processNewLineOutput(line) ;
					}
				} catch (Exception e) {					
				}
			}
		}
		
		public void quit(){
			keepRunning = false ;
		}
		
	}
	
}
